<template>
  <view class="word-card" :class="{ flipped: isFlipped, 'word-card-large': large }">
    <!-- 正面：单词和发音 -->
    <view class="word-card-face word-card-front">
      <view class="word-info">
        <h3 class="word-text">{{ word.text }}</h3>
        <p class="word-pronunciation">{{ word.pronunciation }}</p>
        <button 
          class="btn-pronunciation" 
          @click.stop="playPronunciation" 
          :disabled="!word.pronunciation"
          title="播放发音"
        >
          <i class="icon-volume"></i>
        </button>
      </view>
      
      <view v-if="showProgress && wordStats" class="word-progress">
        <view class="progress-label">掌握程度</view>
        <view class="progress-bar-wrapper">
          <view 
            class="progress-bar"
            :style="{ width: `${wordStats.masteryLevel * 20}%` }"
          ></view>
        </view>
        <view class="progress-text">{{ masteryLevelText }}</view>
      </view>
      
      <view v-if="large" class="word-actions">
        <button class="btn-action" @click.stop="editWord">
          <i class="icon-edit"></i> 编辑
        </button>
        <button class="btn-action" @click.stop="addToWordSet">
          <i class="icon-plus"></i> 添加
        </button>
        <button class="btn-action" @click.stop="markAsLearned">
          <i class="icon-check"></i> 已掌握
        </button>
      </view>
    </view>
    
    <!-- 背面：释义和例句 -->
    <view class="word-card-face word-card-back">
      <view v-if="word.definitions && word.definitions.length > 0" class="word-definitions">
        <h4 class="section-title">释义</h4>
        <ul class="definition-list">
          <li v-for="(definition, index) in word.definitions" :key="index" class="definition-item">
            {{ definition }}
          </li>
        </ul>
      </view>
      
      <view v-if="word.partOfSpeech" class="word-part-of-speech">
        <span class="part-of-speech-tag">{{ word.partOfSpeech }}</span>
      </view>
      
      <view v-if="word.examples && word.examples.length > 0" class="word-examples">
        <h4 class="section-title">例句</h4>
        <ul class="example-list">
          <li v-for="(example, index) in word.examples" :key="index" class="example-item">
            <span class="example-text">{{ example }}</span>
            <button 
              class="btn-example-pronunciation"
              @click.stop="playExamplePronunciation(example)"
              title="播放例句发音"
            >
              <i class="icon-volume-small"></i>
            </button>
          </li>
        </ul>
      </view>
      
      <view v-if="word.synonyms && word.synonyms.length > 0" class="word-synonyms">
        <h4 class="section-title">同义词</h4>
        <view class="synonym-list">
          <span v-for="(synonym, index) in word.synonyms" :key="index" class="synonym-tag">
            {{ synonym }}
          </span>
        </view>
      </view>
      
      <view v-if="word.antonyms && word.antonyms.length > 0" class="word-antonyms">
        <h4 class="section-title">反义词</h4>
        <view class="antonym-list">
          <span v-for="(antonym, index) in word.antonyms" :key="index" class="antonym-tag">
            {{ antonym }}
          </span>
        </view>
      </view>
    </view>
    
    <!-- 翻转控制按钮 -->
    <button 
      class="btn-flip" 
      @click.stop="toggleFlip"
      :class="{ flipped: isFlipped }"
      title="查看详情"
    >
      <i class="icon-flip"></i>
    </button>
  </view>
</template>

<script>
import { ref, computed } from 'vue'

export default {
  name: 'WordCard',
  props: {
    word: {
      type: Object,
      required: true,
      default: () => ({})
    },
    wordStats: {
      type: Object,
      default: null
    },
    showProgress: {
      type: Boolean,
      default: true
    },
    large: {
      type: Boolean,
      default: false
    },
    readOnly: {
      type: Boolean,
      default: false
    }
  },
  emits: ['edit', 'add-to-word-set', 'mark-as-learned', 'pronounce'],
  setup(props, { emit }) {
    const isFlipped = ref(false)
    
    // 翻转卡片
    const toggleFlip = () => {
      isFlipped.value = !isFlipped.value
    }
    
    // 播放单词发音
    const playPronunciation = () => {
      if (!props.word.pronunciation) return
      
      emit('pronounce', props.word.text)
      
      // 模拟播放发音
      try {
        console.log('播放单词发音:', props.word.text)
        // 在实际项目中，这里可以调用真实的发音API
        uni.showToast({
          title: '播放发音中...',
          icon: 'none'
        })
      } catch (error) {
        console.error('播放发音失败:', error)
        uni.showToast({
          title: '播放发音失败',
          icon: 'none'
        })
      }
    }
    
    // 播放例句发音
    const playExamplePronunciation = (example) => {
      try {
        console.log('播放例句发音:', example)
        // 模拟播放例句发音
        uni.showToast({
          title: '播放例句发音中...',
          icon: 'none'
        })
      } catch (error) {
        console.error('播放例句发音失败:', error)
        uni.showToast({
          title: '播放例句发音失败',
          icon: 'none'
        })
      }
    }
    
    // 编辑单词
    const editWord = () => {
      if (!props.readOnly) {
        emit('edit', props.word)
      }
    }
    
    // 添加到单词集
    const addToWordSet = () => {
      if (!props.readOnly) {
        emit('add-to-word-set', props.word)
      }
    }
    
    // 标记为已掌握
    const markAsLearned = () => {
      if (!props.readOnly) {
        emit('mark-as-learned', props.word.id)
        
        // 模拟更新单词状态
        try {
          console.log('标记单词为已掌握:', props.word.id)
          // 在实际项目中，这里可以调用真实的API更新单词掌握度
          uni.showToast({
            title: '已标记为掌握',
            icon: 'success'
          })
        } catch (error) {
          console.error('更新单词掌握度失败:', error)
          uni.showToast({
            title: '更新失败，请重试',
            icon: 'none'
          })
        }
      }
    }
    
    // 计算掌握程度文本
    const masteryLevelText = computed(() => {
      if (!props.wordStats) return ''
      
      const levels = ['很陌生', '陌生', '一般', '熟悉', '很熟悉', '已掌握']
      return levels[props.wordStats.masteryLevel] || ''
    })
    
    return {
      isFlipped,
      toggleFlip,
      playPronunciation,
      playExamplePronunciation,
      editWord,
      addToWordSet,
      markAsLearned,
      masteryLevelText
    }
  }
}
</script>

<style scoped>
.word-card {
  position: relative;
  width: 100%;
  height: 200px;
  perspective: 1000px;
  cursor: pointer;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.word-card-large {
  height: 350px;
}

.word-card-face {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  padding: 20px;
  background: linear-gradient(135deg, #ffffff 0%, #f5f5f5 100%);
}

.word-card-front {
  transform: rotateY(0deg);
  justify-content: space-between;
}

.word-card-back {
  transform: rotateY(180deg);
  background: linear-gradient(135deg, #fafafa 0%, #f0f0f0 100%);
  overflow-y: auto;
}

.word-card.flipped .word-card-front {
  transform: rotateY(180deg);
}

.word-card.flipped .word-card-back {
  transform: rotateY(0deg);
}

.word-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
}

.word-text {
  font-size: 28px;
  font-weight: bold;
  color: #262626;
  margin-bottom: 8px;
}

.word-pronunciation {
  font-size: 16px;
  color: #8c8c8c;
  font-style: italic;
  margin-bottom: 16px;
}

.btn-pronunciation {
  background: none;
  border: none;
  font-size: 24px;
  color: #1890ff;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: all 0.3s;
}

.btn-pronunciation:hover:not(:disabled) {
  background-color: rgba(24, 144, 255, 0.1);
  transform: scale(1.1);
}

.btn-pronunciation:disabled {
  color: #bfbfbf;
  cursor: not-allowed;
}

.word-progress {
  margin-top: 16px;
}

.progress-label {
  font-size: 14px;
  color: #595959;
  margin-bottom: 8px;
}

.progress-bar-wrapper {
  width: 100%;
  height: 8px;
  background-color: #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 4px;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #1890ff 0%, #40a9ff 100%);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 12px;
  color: #8c8c8c;
  text-align: right;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #262626;
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 2px solid #1890ff;
  display: inline-block;
}

.word-definitions,
.word-examples,
.word-synonyms,
.word-antonyms {
  margin-bottom: 16px;
}

.definition-list,
.example-list {
  list-style: none;
  padding: 0;
}

.definition-item {
  font-size: 14px;
  line-height: 1.6;
  color: #595959;
  margin-bottom: 8px;
  padding-left: 16px;
  position: relative;
}

.definition-item:before {
  content: "•";
  position: absolute;
  left: 0;
  color: #1890ff;
  font-weight: bold;
}

.example-item {
  font-size: 14px;
  line-height: 1.6;
  color: #595959;
  margin-bottom: 8px;
  padding-left: 16px;
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.example-item:before {
  content: """;
  position: absolute;
  left: 0;
  top: 0;
  color: #1890ff;
  font-size: 18px;
  font-weight: bold;
}

.example-text {
  flex: 1;
  font-style: italic;
}

.btn-example-pronunciation {
  background: none;
  border: none;
  font-size: 14px;
  color: #1890ff;
  cursor: pointer;
  padding: 4px;
  border-radius: 50%;
  transition: all 0.3s;
  align-self: center;
}

.btn-example-pronunciation:hover {
  background-color: rgba(24, 144, 255, 0.1);
}

.synonym-list,
.antonym-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.synonym-tag,
.antonym-tag {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
}

.synonym-tag {
  background-color: #e6f7ff;
  color: #1890ff;
  border: 1px solid #91d5ff;
}

.antonym-tag {
  background-color: #fff2e8;
  color: #fa8c16;
  border: 1px solid #ffd591;
}

.word-part-of-speech {
  margin-bottom: 16px;
}

.part-of-speech-tag {
  display: inline-block;
  padding: 4px 12px;
  background-color: #f6ffed;
  color: #52c41a;
  border: 1px solid #b7eb8f;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
}

.btn-flip {
  position: absolute;
  top: 12px;
  right: 12px;
  background: none;
  border: none;
  font-size: 18px;
  color: #8c8c8c;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: all 0.3s;
  z-index: 1;
}

.btn-flip:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: #595959;
}

.btn-flip.flipped {
  transform: rotate(180deg);
}

.word-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}

.btn-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background-color: #f0f0f0;
  color: #595959;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-action:hover {
  background-color: #e6f7ff;
  color: #1890ff;
}

/* 图标样式 */
.icon-volume,
.icon-volume-small,
.icon-flip,
.icon-edit,
.icon-plus,
.icon-check {
  display: inline-block;
  width: 1em;
  height: 1em;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.icon-volume {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>');
}

.icon-volume-small {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/></svg>');
}

.icon-flip {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M15.5 5H11v11.17L9.41 15 8 16.41l4 4 4-4-1.41-1.41L13 16.17V5h2.5z"/></svg>');
}

.icon-edit {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/></svg>');
}

.icon-plus {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/></svg>');
}

.icon-check {
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/></svg>');
}

/* 响应式设计 */
@media (max-width: 768px) {
  .word-card {
    height: 180px;
  }
  
  .word-card-large {
    height: 300px;
  }
  
  .word-text {
    font-size: 24px;
  }
  
  .word-pronunciation {
    font-size: 14px;
  }
  
  .section-title {
    font-size: 14px;
  }
  
  .definition-item,
  .example-item {
    font-size: 13px;
  }
  
  .word-actions {
    flex-wrap: wrap;
  }
  
  .btn-action {
    flex: 1;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .word-card {
    height: 160px;
  }
  
  .word-card-large {
    height: 280px;
  }
  
  .word-card-face {
    padding: 16px;
  }
  
  .word-text {
    font-size: 20px;
  }
  
  .word-pronunciation {
    font-size: 12px;
  }
}
</style>